package org.i0o.appliancemanager.ui.win;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import org.i0o.appliancemanager.ui.helper.Clock;
import org.i0o.appliancemanager.ui.helper.Colors;
import org.i0o.appliancemanager.ui.helper.UITools;

public class PosWin extends BaseWin {

	private static final long		serialVersionUID	= 7150264901458246473L;
	// 默认缝隙大小
	private static final int		CONTAINERSIZE		= 5;
	private Object					receiptsCache;

	private static  final String[]	defaultColums		= { "序号", "条码", "商品名称",
			"数量", "单价", "金额"							};
	private static  final int[]		defaultColumsWidth	= { 40, 120, 120, 40,
			80, 80										};
	private JLabel					lblDateTime;
	private JLabel					lblMemberContent;
	private JLabel					lblUserNameContent;
	private JLabel					lblListNumContent;
	private JLabel					lblCountContent;
	private JLabel					lblSumContent;

	private Clock					clock;
	private JButton					btnInsert;
	private JButton					btnPause;
	private JButton					btnRecover;
	private JButton					btnCancel;
	private JButton					btnSale;
	private JButton					btnMember;

	@Override
	protected void initLayout() {
		JPanel backgroundPanel = UITools.createPanel();
		this.add(backgroundPanel);

		GroupLayout layout = new GroupLayout(backgroundPanel);
		backgroundPanel.setLayout(layout);

		JPanel topPanel = UITools.createPanel(new BorderLayout(20, 5));
		topPanel.setBorder(BorderFactory.createEtchedBorder());
		JLabel lblTopTitle = UITools.createLabel(DEFAULT_TITLE,
				topPanel,
				BorderLayout.CENTER);
		UITools.setFont(lblTopTitle, Font.BOLD, 30);

		JPanel infoPanel = UITools.createPanel();
		lblDateTime = UITools.createLabel("2012-8-15 23:50:36");
		this.clock = new Clock(lblDateTime);
		UITools.setFont(lblDateTime, Font.BOLD, 20);

		GridBagLayout infoLayout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		infoPanel.setLayout(infoLayout);

		c.weightx = 0.0;
		UITools.createLabel("单号：", infoPanel, c);
		c.weightx = 1.0;
		this.lblListNumContent = UITools.createLabel("", infoPanel, c);
		c.weightx = 0.0;
		UITools.createLabel("会员：", infoPanel, c);
		c.weightx = 1.0;
		this.lblMemberContent = UITools.createLabel("", infoPanel, c);
		c.weightx = 0.0;
		UITools.createLabel("收银员：", infoPanel, c);
		c.weightx = 1.0;
		this.lblUserNameContent = UITools.createLabel("", infoPanel, c);

		for (Component component : infoPanel.getComponents()) {
			UITools.setFont(component, Font.BOLD, 20);
		}

		// 统计
		GridBagConstraints countC = new GridBagConstraints();
		countC.fill = GridBagConstraints.BOTH;
		countC.insets = new Insets(5, 10, 5, 5);

		JPanel countPanel = UITools.createPanel(new GridBagLayout());
		countPanel.setBorder(BorderFactory.createEtchedBorder());
		countPanel.setBackground(Colors.COUNTPANEL_BACKGROUND);

		countC.weightx = 0.0;
		JLabel lblCount = UITools.createLabel("数量", countPanel, countC);
		UITools.setFont(lblCount, Font.BOLD, 40);

		countC.weightx = 1.0;
		this.lblCountContent = UITools.createLabel("5", countPanel, countC);
		UITools.setFont(lblCountContent, "黑体", Font.BOLD, 80);

		countC.weightx = 0.0;
		JLabel lblSum = UITools.createLabel("金额", countPanel, countC);
		UITools.setFont(lblSum, Font.BOLD, 40);

		countC.weightx = 1.0;
		this.lblSumContent = UITools.createLabel("37.00", countPanel, countC);
		UITools.setFont(lblSumContent, "黑体", Font.BOLD, 80);

		for (Component component : countPanel.getComponents()) {
			component.setForeground(Colors.WHITE);
		}

		// 表格
		DefaultTableModel columsModel = new DefaultTableModel(defaultColums, 0);
		 
		JScrollPane mainTableScroll = new JScrollPane();
		mainTableScroll.setViewportView(mainTable);
		mainTableScroll.getViewport().setBackground(Colors.WHITE);
		
		//表头设置
		 JTableHeader mainTableHeader =  mainTable.getTableHeader();
		 mainTableHeader.setReorderingAllowed(false);
		 
		 mainTable.getColumnModel().getColumn(0).setPreferredWidth(40);
		 Enumeration<TableColumn>  tableColumns =  mainTable.getColumnModel().getColumns();
		 
		 int tableColumnCount = 0;
		 
		 
		 
		 
		JTabbedPane tabbedPanel = new JTabbedPane();

		// 按钮
		JPanel controlPanel = UITools
				.createPanel(new FlowLayout(FlowLayout.LEFT));
		controlPanel.setBorder(BorderFactory.createEtchedBorder());
		this.btnInsert = UITools.createButton("开单", controlPanel);
		this.btnPause = UITools.createButton("挂单", controlPanel);
		this.btnRecover = UITools.createButton("取单", controlPanel);
		this.btnCancel = UITools.createButton("消单", controlPanel);
		this.btnSale = UITools.createButton("打折", controlPanel);
		this.btnMember = UITools.createButton("会员", controlPanel);

		for (Component component : controlPanel.getComponents()) {
			UITools.setFont(component, 20);
		}

		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout
						.createSequentialGroup()
						.addGap(CONTAINERSIZE)
						.addGroup(layout
								.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(topPanel,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addGroup(layout
										.createSequentialGroup()
										.addGroup(layout
												.createParallelGroup(GroupLayout.Alignment.TRAILING)
												.addComponent(infoPanel,
														GroupLayout.DEFAULT_SIZE,
														298,
														Short.MAX_VALUE)
												.addComponent(mainTableScroll))
										.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(layout
												.createParallelGroup(GroupLayout.Alignment.LEADING,
														false)
												.addComponent(lblDateTime,
														GroupLayout.PREFERRED_SIZE,
														250,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(tabbedPanel)))
								.addComponent(controlPanel)
								.addComponent(countPanel))

						.addGap(CONTAINERSIZE)));

		layout.setVerticalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout
						.createSequentialGroup()
						.addGap(CONTAINERSIZE)
						.addGroup(layout
								.createSequentialGroup()
								.addComponent(topPanel,
										GroupLayout.PREFERRED_SIZE,
										60,
										GroupLayout.PREFERRED_SIZE)
								.addGroup(layout
										.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
												false)
										.addComponent(infoPanel,
												GroupLayout.DEFAULT_SIZE,
												40,
												Short.MAX_VALUE)
										.addComponent(lblDateTime,
												GroupLayout.DEFAULT_SIZE,
												40,
												Short.MAX_VALUE))
								.addGroup(layout.createParallelGroup()
										.addComponent(mainTableScroll)
										.addComponent(tabbedPanel))
								.addGap(2)
								.addComponent(controlPanel,
										GroupLayout.PREFERRED_SIZE,
										50,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(countPanel,
										GroupLayout.PREFERRED_SIZE,
										100,
										GroupLayout.PREFERRED_SIZE)

						).addGap(CONTAINERSIZE)));
	}

	@Override
	protected void initListener() {
		this.btnInsert.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				insertHandle();
			}
		});

		this.btnPause.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				pauseHandle();
			}
		});

		this.btnRecover.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				recoverHandle();
			}
		});

		this.btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				cancelHandle();
			}
		});
	}

	@Override
	protected void beforeOpen() {
		this.clock.start();
		super.beforeOpen();
	}

	@Override
	protected void initStyle() {
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);

	}

	private void insertHandle() {
		// TODO Auto-generated method stub

	}

	private void pauseHandle() {
		// TODO Auto-generated method stub

	}

	private void recoverHandle() {
		if (this.receiptsCache == null) {
			super.showErrorAlart("没有挂起的销售单可恢复！");
		}
	}

	private void cancelHandle() {
		int result = showConfirmDialog("是否取消当前录入的销售单？");
		if (result == JOptionPane.YES_OPTION) {

		}
	}

}
